﻿//获取当前带http的域名
function thisHostWithHttp() {
    return window.location.protocol + "//" + window.location.host;
}
//修复ligergrid
function GridNoData(Obj) {
    var grid = $(Obj).ligerGrid();
    var gridHeaderInner = grid.gridview2[0].children[0].children[0];
    $(gridHeaderInner).width($(gridHeaderInner).width() - 40);
    var gridBodyInner = grid.gridbody[0].children[0];
    $(gridBodyInner).width($(gridHeaderInner).width());
    try
    {
        grid.setWidth($(gridHeaderInner).width());
        grid.reRender();
    }
    catch (err) { }
}

//判断是否为空
function IsEmpty(strings) {
    if (typeof (strings) == "undefined" || strings == null || strings == undefined || strings.toString().length <= 0) {
        return true;
    }
    return strings.toString().replace(/(^\s*)|(\s*$)/g, "").length == 0;
}

//判断是否数字
function validationNumber(e, num) {
    var regu = /^[0-9]+\.?[0-9]*$/;
    if (e.value != "") {
        if (!regu.test(e.value)) {
            e.value = e.value.substring(0, e.value.length - 1);
            e.focus();
        } else {
            if (num == 0) {
                if (e.value.indexOf('.') > -1) {
                    e.value = e.value.substring(0, e.value.length - 1);
                    e.focus();
                }
            }
            if (e.value.indexOf('.') > -1) {
                if (e.value.split('.')[1].length > num) {
                    e.value = e.value.substring(0, e.value.length - 1);
                    e.focus();
                }
            }
        }
    }
}

//文本框默认值
function placeholder() {
    $(".ipt").each(function () {
        $(this).val($(this).attr("datavalue")).addClass("inp");
        $(this).focus(function () {
            if ($(this).val() == $(this).attr("datavalue")) {
                $(this).val("").removeClass("inp");
            }

        })
        $(this).blur(function () {
            if ($(this).val() == "" || $(this).val() == $(this).attr("datavalue")) {
                $(this).val($(this).attr("datavalue")).addClass("inp");
            }
        })
    });
}

//时间格式化问题
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份   
        "d+": this.getDate(), //日   
        "H+": this.getHours(), //小时   
        "m+": this.getMinutes(), //分   
        "s+": this.getSeconds(), //秒   
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
        "S": this.getMilliseconds() //毫秒   
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
var ssregex = /^[1-2][0-9][0-9][0-9]-([1][0-2]|0?[1-9])-([12][0-9]|3[01]|0?[1-9]) ([01][0-9]|[2][0-3]):[0-5][0-9]:[0-5][0-9]$/
//精确到秒 若时间是精确的到分的直接返回
function obj2Timess(obj) {
    if (IsEmpty(obj)) return "";
    var time = obj;
    if (time == "1900/1/1 0:00:00")
        return "1900-01-01 00:00:00";
    time = time.toString();
    time = time.replace("T", " ");
    time = time.replace(new RegExp("/", "gm"), "-");
    if (time.length == 23)//去除毫秒
        time = time.substring(0, time.length - 4);
    if ((time.length == 16 && mmregex.test(time)) || (time.length == 19) && ssregex.test(time))//返回分或者秒
        return time;
    var dt = new Date(time.replace(new RegExp("-", "gm"), "/"));
    if (typeof (dt) == "object" && dt) {
        var year = dt.getFullYear();       //年
        var month = dt.getMonth() + 1;     //月
        var day = dt.getDate();            //日
        var hh = dt.getHours();            //时
        var mm = dt.getMinutes();          //分
        var ss = dt.getSeconds();          //秒
        var clock = year + "-";
        if (month < 10) clock += "0";
        clock += month + "-";
        if (day < 10) clock += "0";
        clock += day + " ";
        if (hh < 10) clock += "0";
        clock += hh + ":";
        if (mm < 10) clock += '0';
        clock += mm + ":";
        if (ss < 10) clock += '0';
        clock += ss;
        return (clock);
    }
    return "";
}

var mmregex = /^[1-2][0-9][0-9][0-9]-([1][0-2]|0?[1-9])-([12][0-9]|3[01]|0?[1-9]) ([01][0-9]|[2][0-3]):[0-5][0-9]$/
//精确到分 若时间是精确的到分的直接返回
function obj2Timemm(obj) {
    if (IsEmpty(obj)) return "";
    var time = obj;    
    if (time == "1900/1/1 0:00:00")
        return "1900-01-01 00:00";
    time = time.toString();
    time = time.replace("T", " ");
    time = time.replace(new RegExp("/", "gm"), "-");
    if (time.length == 23)//去除毫秒
        time = time.substring(0, time.length - 4);
    if (time.length == 19)//去除秒
        time = time.substring(0, time.length - 3);
    if (time.length == 16 && mmregex.test(time))//返回分
        return time;
    var dt = new Date(time.replace(new RegExp("-", "gm"), "/"));
    if ((typeof (dt)).toString() == "object" && dt) {
        var year = dt.getFullYear();       //年
        var month = (dt.getMonth() + 1);   //月
        var day = dt.getDate();            //日
        var hh = dt.getHours();            //时
        var mm = dt.getMinutes();          //分
        var clock = year + "-";
        if (month < 10) clock += "0";
        clock += month + "-";
        if (day < 10) clock += "0";
        clock += day + " ";
        if (hh < 10) clock += "0";
        clock += hh + ":";
        if (mm < 10) clock += '0';
        clock += mm;
        return (clock);
    }
    return "";
}
//获取当前时间
function CurentDateTime() {
    var now = new Date();
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日
    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分
    var clock = year + "-";
    if (month < 10) clock += "0";
    clock += month + "-";
    if (day < 10) clock += "0";
    clock += day + " ";
    if (hh < 10) clock += "0";
    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm;
    return (clock);
}
//将checkbox选中value以1,2,3格式返回
function CheckBoxToArrStr(checkboxName) {
    var ids_array = new Array();
    $('input[name="' + checkboxName + '"]:checked').each(function () { ids_array.push($(this).val()); });
    var idstr = ids_array.join(',');
    return idstr;
}
//将1,2,3值与checkbox中项进行匹配选中
function CheckBoxSetVal(checkboxName, vals, isAddClass) {
    if (IsEmpty(checkboxName)) return;
    if (IsEmpty(vals)) return;
    $("input[name=" + checkboxName + "]").removeAttr("checked");
    var valArray = vals.split(",");
    for (var i = 0; i < valArray.length; i++) {
        $("input[name=" + checkboxName + "][value=" + valArray[i] + "]").attr("checked", 'checked');
        $("input[name=" + checkboxName + "][value=" + valArray[i] + "]").prop("checked", 'checked');
    }
    if (isAddClass) {
        $("input[type=checkbox][name=" + checkboxName + "]").parent().removeClass("checked");
        $("input[type=checkbox][name=" + checkboxName + "][checked=checked]").parent().addClass("checked");
    }
}
//为单选按钮赋值 是否增加自定义单选按钮样式
function RadioSetVal(radioName, val, isAddClass) {
    var reg_num = /^\d+$/;
    $("input:radio[name=" + radioName + "]").removeAttr('checked');
    if (isAddClass) {
        $("input:radio[name=" + radioName + "]").parent().addClass("radio_label");
    }
    if (!IsEmpty(val) && reg_num.test(val)) {
        $("input[name=" + radioName + "][value=" + val + "]").attr("checked", 'checked');
        $("input[name=" + radioName + "][value=" + val + "]").prop("checked", 'checked');
    }
    if (isAddClass) {
        $("input[type=radio][name=" + radioName + "]").parent().removeClass("checked");
        $("input[type=radio][name=" + radioName + "][checked=checked]").parent().addClass("checked");
    }
}
//为控件设置val
function ControlSetVal(controlId, val) {
    if (IsEmpty(val))
        $("#" + controlId).val("");
    else
        $("#" + controlId).val(val);
}
//随机数
function getRnd() {
    var mydate = new Date();
    var rnd = Math.floor(Math.random() * 9999) + 1;
    var result = mydate.getHours().toString() + mydate.getMinutes().toString() + mydate.getSeconds().toString() + rnd;
    return result;
}
//检测图片文件名
function checkImageName(fileName) {
    if (IsEmpty(fileName)) return false;
    fileName = fileName.toString().toLowerCase();
    if (fileName.length <= 0) return false;
    if (fileName.indexOf(".") <= 0) return false;
    var fileNameArray = fileName.split('.');
    if (fileNameArray.length < 2) return false;
    var ext = fileNameArray[(fileNameArray.length - 1)];
    if (typeof (ext) == "undefined") return false;
    if (ext == "jpg" || ext == "jpeg" || ext == "png" || ext == "gif" || ext == "bmp")
        return true;
    return false;
}
//检测合法json
function isJSON(str) {
    if (typeof (str) == "string") {
        try {
            var obj = JSON.parse(str);
            if (typeof (obj) == "object" && obj) {
                return true;
            } else {
                return false;
            }
        } catch (e) {
            console.log('error：' + str + '!!!' + e);
            return false;
        }
    }
    return false;
}
